﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Prisa.Entities;
using Prisa.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Prisa.UI
{
    public partial class FrmMttoCategoria : Form
    {
        [Dependency]
        public ICategoriaBL categoriaBL { get; set; }
        bool bandera;

        public FrmMttoCategoria()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void FrmMttoCategoria_Load(object sender, EventArgs e)
        {

        }

        public CATEGORIA CurrentCategoria { get; set; }

        public void Nuevo()
        {
            bandera = true;
            cATEGORIABindingSource.AddNew();
        }

        public void Editar()
        {
            bandera = false;
            cATEGORIABindingSource.DataSource = CurrentCategoria;
            porcentajeTextBox.Text = ((CurrentCategoria.Porcentaje) * 100).ToString();
        }

        private void BtnGuardar_Click(object sender, EventArgs e)
        {
            CATEGORIA currentCategoria = cATEGORIABindingSource.Current as CATEGORIA;
            var msg = "ERROR";
            
            if (currentCategoria != null)
            {
                if (bandera == true)
                {
                    try
                    {
                        currentCategoria.Porcentaje = Convert.ToDecimal(porcentajeTextBox.Text) / 100;
                        currentCategoria.Descripcion = descripcionTextBox.Text.ToUpper();
                        categoriaBL.AddCategoria(currentCategoria);
                        MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                        
                }
                if (bandera == false)
                {
                    try
                    {
                        currentCategoria.Porcentaje = Convert.ToDecimal(porcentajeTextBox.Text) / 100;
                        currentCategoria.Descripcion = descripcionTextBox.Text.ToUpper();
                        categoriaBL.UpdateCategoria(currentCategoria);
                        MessageBox.Show("Modificación Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    
                }
            }
        }

        private void porcentajeTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (
                !char.IsNumber(e.KeyChar) &&
                !char.IsDigit(e.KeyChar) &&
                e.KeyChar != (char)Keys.Back &&
                e.KeyChar != (char)Keys.Enter
                )
            {
                e.Handled = true;
            }

            if (e.KeyChar == '.' || e.KeyChar == '\b')
            {
                e.Handled = false;
            }
        }

        private void descripcionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = true;
            }
            else if (!Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (!Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
